﻿using nplJobs.DB;
using nplJobsCommon.Employeer;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace nplJobsDAL.Select
{
    public class SelectEmployeer : DataAccessBase
    {
        public Guid EmployeerID { get; set; }

        public SelectEmployeer()
        {
            StoredProcedureName = StoredProcedure.Name.spSelectEmployeer.ToString();
        }

        #region GetEmployeer 
        public List<Employeer> GetEmployeer()
        {
            DataBaseHelper dbHelper;
            DataSet dsEmployeer;

            try
            {
                SelectCityParameters selecCityParamenters = new SelectCityParameters(this.EmployeerID);
                dbHelper = new DataBaseHelper(StoredProcedureName);
                dbHelper.Parameters = selecCityParamenters.Parameters;
                dsEmployeer = dbHelper.Run(base.ConnectionString, dbHelper.Parameters);
                return GetList(dsEmployeer);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public  List<Employeer> GetList()
        {
            CustomParameterList cpList = new CustomParameterList();

            cpList.Add(new CustomParameter("@EmployeerID", this.EmployeerID, DbType.Guid));

            object paramval = null;
            DBHelper db = new DBHelper();
            DataTable dt = db.ExecuteQueryToDataTable(cpList.ParameterNames, cpList.ParameterValues, cpList.ParameterTypes, ref paramval, null, "spSelectEmployeer");

            List<Employeer> results = new List<Employeer>();
            foreach (DataRow dr in dt.Rows)
            {
                Employeer objJobList = new Employeer();

                MapperBase.GetInstance().MapItem(objJobList, dr); ;
                results.Add(objJobList);
            }
            return results;

        }

        #endregion



        #region SQL parameters for selecting cities by country
        public class SelectCityParameters
        {
            public int CkeckUnique;
            private SqlParameter[] _parameters;

            public SelectCityParameters(Guid employeerID)
            {
                this.EmployeerID = employeerID;
                Build();
            }

            #region spAddContact parameters
            /*
         
         */
            #endregion

            private void Build()
            {
                SqlParameter[] parameters = 
                {
                    new SqlParameter("@EmployeerID", EmployeerID),
                };

                Parameters = parameters;
            }

            public Guid EmployeerID { get; set; }

            public SqlParameter[] Parameters
            {
                get { return _parameters; }
                set { _parameters = value; }
            }
        }
        #endregion
        
        
    //    :DataAccessBase
    //{

    //    public SelectEmployeer()
    //    {
    //        StoredProcedureName = StoredProcedure.Name.spSelectEmployeer.ToString();
    //    }

    //    #region GetEmployeer
    //    public List<Employeer> GetEmployeer()
    //    {
    //        DataBaseHelper dbHelper;
    //        DataSet dsEmployeer;

    //        try
    //        {
    //            dbHelper = new DataBaseHelper(StoredProcedureName);
    //            dsEmployeer = dbHelper.Run(base.ConnectionString);
    //            return GetList(dsEmployeer);
    //        }
    //        catch (Exception)
    //        {

    //            throw;
    //        }
    //    } 
    //    #endregion

       #region GetList
        private List<Employeer> GetList(DataSet dsEmployeer)
        {
            List<Employeer> employeer;
            try
            {
                employeer = (from n in dsEmployeer.Tables[0].AsEnumerable()
                                select new Employeer
                                 {
                                     EmployeerID = n.Field<Guid>("EmployeerID"),
                                     CompanyName = n.Field<string>("CompanyName"),
                                     LocationID = n.Field<int>("LocationID"),
                                     ContactPersonID = n.Field<Int32>("ContactPersonID"),
                                     EmployeerTypeID = n.Field<Int32>("EmployeerTypeID"),
                                     IndustryTypeID = n.Field<Int32>("IndustryTypeID"),
                                     CompanyDescription = n.Field<string>("CompanyDescription"),
                                     Address = n.Field<string>("Address"),
                                     CompanyImage = n.Field<string>("CompanyImage"),
                                     CompanyLogo = n.Field<string>("CompanyLogo"),
                                     EmployeeRangeID = n.Field<int>("EmployeeRangeID"),
                                     HomePageUrl = n.Field<string>("HomePageUrl"),
                                     IsDeleted = n.Field<bool>("IsDeleted"),
                                     IsActivated = n.Field<bool>("IsActivated"),
                                     Phone = n.Field<string>("Phone"),
                                     MobilePhone = n.Field<string>("MobilePhone"),
                                     Fax = n.Field<string>("Fax")

                                 }).ToList();

                return employeer;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        } 
        #endregion
    }
}
